package com.marketingexpert.controller.friendmgnt;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.UserType;
import com.marketingexpert.domain.permission.Resource;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.Resources;
import com.marketingexpert.util.WebUtils;

public class GetFollowedMeController implements Controller {

	private static final int COUNT = 10;
	
	private String success = null;
	
	private UserService userService = null;
	
	private SnsServiceFactory snsServiceFactory = null;
	
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		
		
		int cursor = Integer.parseInt(request.getParameter("cursor"));
		String userId = request.getParameter("userId");
		
		
		int realCursor = cursor * COUNT - COUNT;
		try {
	
			com.marketingexpert.domain.User myUser = userService.queryUserByUId(userId);			
		String token = myUser.getAccessToken();
		String secret = myUser.getSecret();
		String myId = String.valueOf(myUser.getUserId());
		
		SnsService weboauth = WebUtils.getSnsService(myUser, getSnsServiceFactory());
		
	
		List<com.marketingexpert.service.snsmgnt.User> friends = weboauth.getFollowedMeStatues(token, secret,myId,realCursor);
		Map<String, Object> friendShipMap = new HashMap<String, Object>();
		
		for(Iterator<com.marketingexpert.service.snsmgnt.User> it = friends.iterator(); it.hasNext();) {
			
			com.marketingexpert.service.snsmgnt.User u = it.next();
			
			boolean existsFriendShip = weboauth.existsFriendship(token, secret, myId, String.valueOf(u.getId()));
			
				friendShipMap.put("existsFriendShip", existsFriendShip);
			friendShipMap.put("follower", u);
		}		
		
		int friendsAccounts = weboauth.getMyFollowerCount(myUser.getAccessToken(), myUser.getSecret(), String.valueOf(myUser.getUserId()));
		
		int pageAccounts = -1;
		int beyond = friendsAccounts % COUNT;
		
		if(beyond == 0) {
			pageAccounts = friendsAccounts / COUNT;
		} else {
			pageAccounts = friendsAccounts / COUNT + 1;
		}
		
		
		request.setAttribute("page", cursor);
		request.setAttribute("pageAccounts", pageAccounts);
		request.setAttribute("followers", friends);
		request.setAttribute("userId", userId);		
		return new ModelAndView(getSuccess());
		} catch(MarketingExpertException e) {			
			request.setAttribute("requestError", Resources.getString("OperateError") + e.getMessage());
			return new ModelAndView(getSuccess());
		}		
	}
	
	public String getSuccess() {
		return success;
	}







	public void setSuccess(String success) {
		this.success = success;
	}







	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}
	
	

}
